#!/bin/bash  -e

# different from monitors.sh, which run item n times. this run items once, with input a list.
function info() {
echo Usage: `basename $0` '[-n(ame) -c(pu) -m(em)]' cmd_next args_next cmd_monitor list_monitor
exit 65
}

while getopts  "n:m:c:i:p:h" opts
do
        case  $opts  in
		n) name=$OPTARG;;
		c) cpu=$OPTARG;;
		m) mem=$OPTARG;;
        i) interval=$OPTARG;;
		p) out_prefix=$OPTARG;;
		h) echo subq.sh -p1 -m1 monitors.sh -nsort -c2 -m2 "\"'sam2b_samtools.sh -t2'\"" 1.sam.list aln_bwa_bt.sh 1.cut.list; info;;
		*) info;;
        esac
done
shift $(($OPTIND - 1))


if [ $# -lt 3 ]; then info; fi

. $var

IFS=$'\n'

cmd=$3
list=$4
cmd_next=$1
list_next=$2

num=`wc -l $list|cut -f1 -d' '`

while true; do
	i=0
	for item in $(cat $list); do # if $list has more than 1 column, it will go wrong. item like item1 item2 in a row, it should be only one item. using IFS=$'\n' to modify.
		if
		cat $pid_log|grep -F "$cmd $item done" >/dev/null
		then
			echo `hostname` "$cmd $item done"
			i=$((i+1))
		else

			echo `hostname` "$cmd $item undone"
			sleep 30
			break
		fi
	done
	
	if test $i -eq $num; then
		$cmd_next $list_next
	break
	fi 
	
done

wait

. $cmd_done